package com.iss.umservice.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.iss.iframework.util.StringUtil;
import com.iss.iframework.web.ResultCall;
import com.iss.iframework.web.support.tld.pagination.Navigation;
import com.iss.iframework.web.support.tld.pagination.PageUtil;
import com.iss.iframework.web.support.tld.pagination.Pagination;
import com.iss.umservice.bean.ResourceBean;
import com.iss.umservice.bean.RoleBean;
import com.iss.umservice.bean.SystemBean;
import com.iss.umservice.manager.ResourceManager;
import com.iss.umservice.manager.RoleManager;
import com.iss.umservice.manager.SystemManager;

/**
 * 角色信息管理控制器
 * 
 * @author suxiaoping
 * @date 2012-8-13
 */
@Controller
@RequestMapping("/resource/*")
public class ResourceController
{
    @Autowired
    private ResourceManager resourceManager;
    
    @Autowired
    
    private RoleManager roleManager;
    @Autowired
    private SystemManager systemManager;
    
    /**
     * 查询的资源
     * 
     * @param roleName //资源名称
     * @return  void
     */
    @RequestMapping
    public void queryResourceInfos(ResourceBean resourceBean,
            String pages, Map<String, Object> model)
    {
        //统计资源个数
        int count = resourceManager.countResourceInfo(resourceBean);
        
        //分页
        Pagination pagination = PageUtil.createPage(pages,
                count,
                "../resource/queryResourceInfos.html");
        
        //查询资源记录
        List<ResourceBean> beanList = resourceManager.queryResourceInfos(resourceBean,
                pagination.getOffset(),
                pagination.getRowSize());
        
        
        List<SystemBean> systemBeanList = systemManager.querySystemList();
        //条件放入分页当中
        pagination.addCondition("resourceName", resourceBean.getResourceName());      
        pagination.addCondition("systemId", resourceBean.getSystemId());
        pagination.addCondition("resourceType", resourceBean.getResourceType());
        
        //存入MAP
        model.put("systemBeanList", systemBeanList);
        model.put("resourceBean", beanList);
        model.put("systemId", resourceBean.getSystemId());
        model.put("resourceType", resourceBean.getResourceType());
        
        model.put(Navigation.PAGINATION, pagination);
        
    }
    
    /**
     * 跳转界面
     * @return
     */
    @RequestMapping
    public void insertResourceInfo()
    {
        
    }
    
    /**
     * 增加资源信息
     * 
     * @param bean 资源实体类
     * @param model map
     * @return
     */
    @RequestMapping
    public void addResourceInfo(ResourceBean resourceBean)
    {
        //鐢熸垚璧勬簮ID
        String resourceId = StringUtil.getPrimarySessionId();
        
        //鑾峰彇褰撳墠绯荤粺ID

        resourceBean.setSystemId(systemManager.getSystemId());
        //璁剧疆ID
        resourceBean.setResourceId(resourceId);
        resourceBean.setResourceType("MENU");
              
        
        //添加岗位信息
        resourceManager.addResourceInfo(resourceBean);
        
        RoleBean roleBean = new RoleBean();
        roleBean.setRoleId(StringUtil.getPrimarySessionId());
        roleBean.setResourceId(resourceBean.getResourceId());
        roleBean.setRoleName(resourceBean.getResourceName());
        roleBean.setRoleDesc(resourceBean.getResourceDesc());
        roleBean.setSystemId(resourceBean.getSystemId());
        roleManager.addRoleInfo(roleBean);
        
        ResultCall.makeSuccess("../resource/queryResourceInfos.html");
        
    }
    
    /**
     * 修改跳转界面
     * @return
     */
    @RequestMapping
    public String updateResourceInfo(String resourceId,
            Map<String, Object> model)
    {
        //根据ID查询资源信息
        ResourceBean resourceBean = resourceManager.queryResourceInfoById(resourceId);
        
        //存入Map
        model.put("resourceBean", resourceBean);
        
        //跳转的界面
        return "/resource/modifyResourceInfo";
    }
    
    /**
     * 修改资源信息与资源对应信息
     * 
     * @param bean 岗位实体类
     * @param model map
     * @return
     */
    @RequestMapping
    public void modifyResourceInfo(ResourceBean resourceBean)
    {
        //修改岗位信息
        resourceManager.updateResourceInfo(resourceBean);
        
        
        RoleBean roleBean = new RoleBean();
        roleBean.setRoleName(resourceBean.getResourceName());
        roleBean.setRoleDesc(resourceBean.getResourceDesc());
        roleBean.setResourceId(resourceBean.getResourceId());
        roleManager.updateRoleName(roleBean);
        
        //跳转的界面
        ResultCall.makeSuccess("../resource/queryResourceInfos.html");
        
    }
    
    /**
     * 删除资源信息
     * 
     * @param roleId 资源ID
     * @param model map
     * @return
     */
    @RequestMapping
    public void delResourceInfoById(String resourceId)
    {
        //修改岗位信息
        resourceManager.delResourceInfoById(resourceId);
        
        //跳转的界面
        ResultCall.makeSuccess("../resource/queryResourceInfos.html");
        
    }
    
    /**
     * 校验资源名称是否已存在
     * @param stationName
     * @param response
     * @throws UnsupportedEncodingException
     */
    @RequestMapping
    public void checkResourceName(String resourceName, HttpServletResponse response) throws UnsupportedEncodingException
    {
        resourceName = URLDecoder.decode(resourceName,"UTF-8");
       String isExist = resourceManager.checkResourceName(resourceName);
       PrintWriter writer = null;
       try
       {
           response.setCharacterEncoding("UTF-8");
           writer = response.getWriter();
           writer.write(isExist);
       }
       catch (Exception e) {
    }
       
    }
}
